// pages/my/service-order/service-order.js
const controllers = require('../../controllers/controllerOrder').controller
const controllerIndex = require("../../controllers/controllerIndex").controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isProduct: 1,
    navlist: ['全部', '待取货', '已完成', '待拼团', '已关闭'],
    proOrderTypeList: ['线上商品订单', '线下商品订单', '拼团订单', '线上会员卡订单', '线下会员卡订单', ],
    traOrderTypeList: ['云商品订单', '店铺爆款', '欢乐拼', '超值抵扣券', '超值礼包'],
    vipOrderTypeList: ['全部', '套餐卡', '充值卡', '会员卡'],
    lineOrderMailList: ['全部', '到店自取', '直邮到家'],
    lineOrderStateList: ['全部', '待核销', '已完成'],
    shopHotOrderStateList: ['全部', '待核销', '已核销'],
    activenav: 0,
    orderList: [],
    imgurl: '',
    navchoice: '',
    navall: false,
    sales: [],
    salesname: [],
    isshow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    singlesales: '',
    singleordno: '',
    singlesalesname: '',
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
    type: 1,
    lastPage: false,
    selectIndex: 0,
    selectStaffIndex: "",
    startTime: '',
    endTime: '',
    storeInfo: "",
    isMail: 0,
    state: 0,
    useState: 0,
    orderTypes: '',
    vipType: 0,
    isFromOrder: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  //改变开始时间
  //  bindStartChange(e){
  //   this.setData({
  //     startTime:e.detail.value
  //   })
  //   if(this.data.selectIndex==1){
  //     this.getLowOrderList()
  //   }else{
  //     this.orderList()
  //   }
  // },

  //改变结束时间
  // bindEndChange(e){
  //   this.setData({
  //     endTime:e.detail.value
  //   })
  //   if(this.data.selectIndex==1){
  //     this.getLowOrderList()
  //   }else{
  //     this.orderList()
  //   }
  // },

  // 取消订单
  cancleOrder(e) {
    let that = this
    let value = {}
    let text = ""
    let payType = e.currentTarget.dataset.paytype
    value.shopId = wx.getStorageSync('shopId')
    value.orderNo = e.currentTarget.dataset.id
    if (payType == 1) {
      text = "该订单属于微信支付,系统不会自动退款需要线下打款操作"
    } else {
      text = "确定取消该订单吗？"
    }
    wx.showModal({
      title: "提示",
      content: text,
      success(res) {
        if (res.confirm) {
          controllers.cancelOrder(value).then(res => {
            wx.showToast({
              title: '取消成功',
            })
            if (that.data.selectIndex == 0 || that.data.selectIndex == 2) {
              that.orderList()
            } else {
              that.getLowOrderList()
            }

          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //获取订单信息
  getShopMsg() {
    console.log(this.data.isProduct);
    const value = {
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId')
    }
    if (this.data.isProduct) {
      controllerIndex.orderStatistics(value).then(res => {
        this.setData({
          storeInfo: res.data
        })
      })
    } else {
      controllerIndex.bagOrderStatistics(value).then(res => {
        this.setData({
          storeInfo: res.data
        })
      })
    }
  },





  changeOrderType(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      pageNumber: 1,

    })
    if (this.data.isProduct) {
      switch (index) {
        case 0: //线上
          this.setData({
            type: 1,
            orderTypes: ''
          })
          this.orderList()
          break;
        case 1: //线下
          this.setData({
            type: 1,
          })
          this.getLowOrderList()
          break;
        case 2: //拼团
          this.setData({
            type: 1,
            orderTypes: 2 // (拼团支付)
          })
          this.orderList()
          break;
        case 3: //线上会员卡
          this.setData({
            line: 0,
          })
          this.vipOrderList()
          break;
        case 4: //线下会员卡
          this.setData({
            line: 1,
          })
          this.vipOrderList()
          break;

      }
    } else {
      switch (index) {
        case 0: //云商品
          this.setData({
            type: 2,
          })
          this.orderList()
          break;
        default: //商圈活动
          this.setData({
            type: index + 1,
          })
          this.couponBagList()
      }
    }
  },
  lineOrderChange(e) {
    let isMail = e.currentTarget.dataset.ismail
    let state = e.currentTarget.dataset.state

    if (typeof isMail == "number") {
      this.setData({
        isMail,
      })
    }
    if (typeof state == "number") {
      this.setData({
        state,
        useState: state
      })
    }
    this.setData({
      pageNumber: 1,
    })
    this.orderList()

  },

  // 店铺爆款useState
  shopHotOrderChange(e) {
    let state = e.currentTarget.dataset.state
    this.setData({
      useState: state,
      pageNumber: 1,
    })
    this.couponBagList()
  },

  // 线上会员卡vipType
  vipOrderChange(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      pageNumber: 1,
      vipType: type
    })
    this.vipOrderList()
  },

  //获取商圈活动列表
  couponBagList(isFirst = true) {
    if (isFirst) {
      this.setData({
        lastPage: false,
        pageNumber: 1,
        orderList: []
      })
    }
    if (this.data.lastPage) return
    let pageNumber = this.data.pageNumber
    let value = {}
    value.pageNumber = pageNumber
    value.types = this.data.type
    value.shopId = wx.getStorageSync('shopId')
    //店铺爆款useState 全部(不传) 0(待核销) 1(已核销)
    if (this.data.selectIndex == 1 && this.data.useState != 0) {
      value.useState = this.data.useState - 1
    }
    controllerIndex.couponBagPage(value).then(res => {
      let orderList = this.data.orderList
      orderList = orderList.concat(res.data.list)
      pageNumber += 1
      this.setData({
        orderList,
        pageNumber,
        lastPage: res.data.lastPage
      })

    })
  },

  //获取会员卡订单
  vipOrderList(isFirst = true) {
    if (isFirst) {
      this.setData({
        lastPage: false,
        pageNumber: 1,
        orderList: []
      })
    }
    if (this.data.lastPage) return
    let pageNumber = this.data.pageNumber
    let value = {}
    value.pageNumber = pageNumber
    value.line = this.data.line
    value.shopId = wx.getStorageSync('shopId')
    //type 全部(不传) 套餐卡(0) 充值卡(1) 会员卡(5)
    switch (this.data.vipType) {
      case 0:
        break;
      case 1:
        value.type = 0
        break;
      case 2:
        value.type = 1
        break;
      case 3:
        value.type = 5
        break
    }
    controllerIndex.vipOrderPage(value).then(res => {
      let orderList = this.data.orderList
      orderList = orderList.concat(res.data.list)
      orderList.forEach(item=>{
        switch (item.type) {
          case 0:
            item.typeName = "套餐卡"
            break;
          case 1:
            item.typeName = "充值卡"
            break;
          case 5:
            item.typeName = "会员卡"
            break;
        }
      })
      pageNumber += 1
      this.setData({
        orderList,
        pageNumber,
        lastPage: res.data.lastPage
      })

    })
  },

  //获取线下商品订单列表
  getLowOrderList() {
    let value = {}
    value.pageNumber = 1
    value.shopId = wx.getStorageSync('shopId')
    value.startTime = this.data.startTime
    value.endTime = this.data.endTime
    if (this.data.isFromOrder) {
      value.shopAccountId = wx.getStorageSync('shopAccountId')
    }
    controllerIndex.selectOrder(value).then(res => {
      this.setData({
        orderList: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  //按钮事件
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.index == 0) {
      this.setData({
        isshow: false
      })
    } else {
      //提交
      controllers.serviceComplete({
        orderNo: this.data.singleordno,
        shopId: wx.getStorageSync('shopId'),
        shopAccountId: this.data.singlesales,
        type: 1
      }).then(res => {
        console.log(res);
        this.setData({
          isshow: false,
          pageNumber: 1,
          pageSize: 10,
          isEnd: false,
          orderList: []
        })
        this.orderList(this.data.navchoice)

      })
    }
  },
  //选择销售
  choice(e) {
    console.log(e);

    this.setData({
      singlesales: this.data.sales[e.detail.value].id,
      singlesalesname: this.data.sales[e.detail.value].nick
    })
  },
  //打开选择销售弹框
  toChoiceSales() {
    //提交
    controllers.serviceComplete({
      orderNo: this.data.singleordno,
      shopId: wx.getStorageSync('shopId'),
      shopAccountId: this.data.staffId || '',
      type: 1
    }).then(res => {
      console.log(res);
      this.setData({
        isshow: false,
        pageNumber: 1,
        pageSize: 10,
        isEnd: false,
        orderList: []

      })
      this.orderList(this.data.navchoice)
    })
  },


  //跳转到订单详情页
  toOrderDetail(e) {
    let item = e.currentTarget.dataset.item
    let index = this.data.selectIndex
    if (this.data.isProduct) { //店铺
      switch (index) {
        case 0:
        case 2:
          wx.navigateTo({
            url: '/pages/order/productOrder-detail/productOrder-detail?id=' + item.orderNo + '&state=' + item.state
          })
          break;
        case 1:
          wx.navigateTo({
            url: '/pages/settlement/OrderDetails/OrderDetails?id=' + item.id,
          })
          break;
        case 3:
        case 4:
          wx.navigateTo({
            url: '/pages/settlement/CardInfo/VIPCardInfo?id=' + item.accountVipId||item.id
          })
          break;
      }
    } else { //商圈
      switch (index) {
        case 0:
          wx.navigateTo({
            url: '/pages/order/cloudOrder-detail/cloudOrder-detail?id=' + item.orderNo + '&state=' + item.state
          })
          break;
        default:
          item = JSON.stringify(item)
          wx.navigateTo({
            url: '/pages/order/couponBagOrder-detail/couponBagOrder-detail?item=' + item,
          })
      }
    }

  },

  //获取订单列表
  orderList(state) {
    let value = {
      pageNumber: 1,
      pageSize: this.data.pageSize,
      shopId: wx.getStorageSync('shopId'),
      shopAccountId: wx.getStorageSync('shopAccountId'),
      type: this.data.type,
      startTime: this.data.startTime,
      endTime: this.data.endTime,
    }
    if (this.data.type == 1) {
      if (this.data.isMail != 0) {
        value.isMail = this.data.isMail - 1
      }
      switch (this.data.state) {
        case 0:
          break;
        case 1:
          value.state = 2 //待核销
          break;
        case 2:
          value.state = 8 //已完成
          break
      }
      value.orderTypes = this.data.orderTypes // 2(拼团支付)
    }
    controllers.orderList(value).then(res => {
      this.setData({
        orderList: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  getMore() {
    if (this.data.lastPage == false) {
      if (this.data.selectIndex != 1) {
        let pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: pageNumber
        })
        let value = {
          pageNumber: pageNumber,
          pageSize: this.data.pageSize,
          shopId: wx.getStorageSync('shopId'),
          shopAccountId: wx.getStorageSync('shopAccountId'),
          type: this.data.type,
          startTime: this.data.startTime,
          endTime: this.data.endTime,
        }
        if (this.data.type == 1) {
          if (this.data.isMail != 0) {
            value.isMail = this.data.isMail - 1
          }
          switch (this.data.state) {
            case 0:
              break;
            case 1:
              value.state = 2 //待核销
              break;
            case 2:
              value.state = 8 //已完成
              break
          }
          value.orderTypes = this.data.orderTypes // 2(拼团支付)
        }
        controllers.orderList(value).then(res => {
          let list = this.data.orderList
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            orderList: list3,
            lastPage: res.data.lastPage
          })
        })
      } else {
        let pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: pageNumber
        })
        let value = {}
        value.pageNumber = pageNumber
        value.shopId = wx.getStorageSync('shopId')
        value.startTime = this.data.startTime
        value.endTime = this.data.endTime
        controllerIndex.selectOrder(value).then(res => {
          let list = this.data.orderList
          let list2 = res.data.list
          let list3 = list.concat(list2)
          this.setData({
            orderList: list3,
            lastPage: res.data.lastPage
          })
        })
      }
    }
  },


  onLoad: function (options) {
    if (options.isFromOrder) {
      this.setData({
        isFromOrder: true
      })
    }
    this.setData({
      isProduct: Number(options.isProduct),
      imgurl: app.globalData.IMG_URL,
      sales: wx.getStorageSync('sales')
    })

    let salesname = []
    let sales = wx.getStorageSync('sales')
    if (sales) {
      sales.forEach(function (item, index) {
        salesname.push(item.nick)
      })
      this.setData({
        salesname: salesname
      })
    }

    this.getStaffList()
    this.getShopMsg()
    this.setData({
      orderList: [],
      isEnd: false,
      pageNumber: 1,
    })
  },


  onShow: function () {
    const selectIndex = this.data.selectIndex
    if (this.data.isProduct) {
      switch (selectIndex) {
        case 0: //线上
          this.orderList()
          break;
        case 1: //线下
          this.getLowOrderList()
          break;
      }
    } else {
      switch (selectIndex) {
        case 0: //云商品
          this.orderList()
          break;
        default: //商圈活动
          this.couponBagList()
      }
    }
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const selectIndex = this.data.selectIndex
    if (!this.data.isProduct && selectIndex != 0) { //商圈除云商品
      this.couponBagList(false)
    } else if (this.data.isProduct && (selectIndex == 3 || selectIndex == 4)) { //线上线下会员卡
      this.vipOrderList(false)
    } else {
      this.getMore()
    }
  },


  //关闭员工选框
  closeOpen() {
    this.setData({
      staffBox: false
    })
  },

  //显示弹框
  showModal: function (e) {
    this.setData({
      singleordno: e.currentTarget.id
    })
    let index = e.currentTarget.dataset.index
    this.setData({
      showModalStatus: true
    })
  },
  // 隐藏弹框
  hideModal: function () {
    this.setData({
      showModalStatus: false
    })
  },

  //选择员工
  selectStaff(e) {
    let index = e.currentTarget.dataset.index
    console.log(e)
    if (index === this.data.selectStaffIndex) {
      this.setData({
        selectStaffIndex: "",
        staffId: "",
        staffName: ""
      })
    } else {
      this.setData({
        selectStaffIndex: index
      })
    }
  },

  //确定员工
  defineStaff() {
    if (this.data.selectStaffIndex || this.data.selectStaffIndex === 0) {
      this.setData({
        staffId: this.data.list[this.data.selectStaffIndex].id,
        staffName: this.data.list[this.data.selectStaffIndex].nick
      })
    }
    this.toChoiceSales()
    this.hideModal()
  },

  //跳转员工详情
  selectStaffDetail(e) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let value = this.data.list[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail=' + encodeURIComponent(JSON.stringify(value)),
    })
  },

  //获取员工列表
  getStaffList() {
    let shopId = wx.getStorageSync('shopId')
    controllers.getStaffList({
      shopId: shopId
    }).then(res => {
      this.setData({
        list: res.data
      })
    })
  },
})